import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import componentsRouter from './modules/components'
import chartsRouter from './modules/charts'
import tableRouter from './modules/table'
import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 * 不需要权限就可以访问的路由
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register/register'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: '系统首页',
        meta: { title: '系统首页', icon: 'dashboard', affix: true }
      }
    ]
  },



  {
    path: '/member',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/member/index'),
        name: '成员',
        meta: { title: '成员', icon: 'tab', affix: true }
      }
    ]
  },

  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  },
  {
    path: '/target',
    component: Layout,
    redirect: '/target/entering',
    alwaysShow: true,
    name: '指标管理',
    meta: {
      title: '指标管理',
      icon: 'el-icon-s-help'
    },
    children: [
      {
        path: 'entering',
        component: () => import('@/views/target/entering'),
        name: '指标录入',
        meta: { title: '指标录入', icon: 'edit' }
      },
    ]
  },
  // {
  //   path: '/charts',
  //   component: Layout,
  //   redirect: '/charts/keyboard',
  //   alwaysShow: true,
  //   name: '计划模块',
  //   meta: {
  //     title: '计划模块',
  //     icon: 'chart'
  //   },
  //   children: [
  //     {
  //       path: 'keyboard',
  //       component: () => import('@/views/charts/keyboard'),
  //       name: '单位计划录入',
  //       meta: { title: '单位计划录入', icon: 'edit' }
  //     },
  //     {
  //       path: 'line',
  //       component: () => import('@/views/charts/line'),
  //       name: '调减计划录入',
  //       meta: { title: '调减计划录入', icon: 'edit' }
  //     },
  //   ]
  // },
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 * 需要权限才可以访问的路由
 */
export const asyncRoutes = [
  {
      path: '/budget',
      component: Layout,
      redirect: '/budget/payment',
      alwaysShow: true, // will always show the root menu
      name: '预算编报',
      meta: {
        title: '预算编报',
        icon: 'icon',
        roles: ['company','department','business','budget'] // you can set roles in root nav
      },
      children: [
        {
          path: 'company',
          component: () => import('@/views/budget/company/index'),
          name: '公司信息',
          meta: {
            title: '公司信息',
            roles: ['company'] // or you can only set roles in sub nav
          }
        },
        {
          path: 'control',
          component: () => import('@/views/budget/control/index'),
          name: '控制数',
          meta: {
            title: '控制数',
            roles: ['company','department','budget'] // or you can only set roles in sub nav
          }
        },
        {
          path: 'payment',
          component: () => import('@/views/budget/payment/index'),
          name: '预算支出',
          meta: {
            title: '预算支出',
            roles: ['company','department','business','budget'] // or you can only set roles in sub nav
          }
        },
        {
          path: 'income',
          component: () => import('@/views/budget/income/index'),
          name: '预算收入',
          meta: {
            title: '预算收入',
            roles: ['company'] // or you can only set roles in sub nav
          }
        },
        {
          path: 'summary',
          component: () => import('@/views/budget/summary/index'),
          name: '收支总表',
          meta: {
            title: '收支总表',
            roles: ['company','department','business','budget'] // or you can only set roles in sub nav
          }
        },
        ],
     },
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/page',
    alwaysShow: true, // will always show the root menu
    name: '许可',
    meta: {
      title: '许可',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/permission/page'),
        name: '页面权限',
        meta: {
          title: '页面权限',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'directive',
        component: () => import('@/views/permission/directive'),
        name: '指令许可',
        meta: {
          title: '指令许可'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'role',
        component: () => import('@/views/permission/role'),
        name: '角色权限',
        meta: {
          title: '角色权限',
          roles: ['admin']
        }
      }
    ]
  },

  /** when your routing map is too long, you can split it into small modules **/
  componentsRouter,
  chartsRouter,
  // nestedRouter,
  // tableRouter,

  // {
  //   path: '/example',
  //   component: Layout,
  //   redirect: '/example/list',
  //   name: '样例',
  //   meta: {
  //     title: '样例',
  //     icon: 'el-icon-s-help'
  //   },
  //   children: [
  //     {
  //       path: 'create',
  //       component: () => import('@/views/example/create'),
  //       name: '创建文章',
  //       meta: { title: '创建文章', icon: 'edit' }
  //     },
  //     {
  //       path: 'edit/:id(\\d+)',
  //       component: () => import('@/views/example/edit'),
  //       name: '编辑文章',
  //       meta: { title: '编辑文章', noCache: true, activeMenu: '/example/list' },
  //       hidden: true
  //     },
  //     {
  //       path: 'list',
  //       component: () => import('@/views/example/list'),
  //       name: '文章列表',
  //       meta: { title: '文章列表', icon: 'list' }
  //     }
  //   ]
  // },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
